package com.yl.controller;

import com.yl.entity.AppCarouselEntity;
import com.yl.service.AppCarouseService;
import com.yl.utils.R;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 首页轮播图
 */
@RestController
@RequestMapping("/carousel")
public class AppCarouselController {

    @Resource
    private AppCarouseService appCarouseService;

    /**
     * 获取所有轮播图数据
     */
    @PostMapping("/getCarouselList")
    public R getCarouselList(){
        List<AppCarouselEntity> list = appCarouseService.list().stream().filter(e -> e.getStatus() == 0).toList();
        return R.ok("获取轮播图成功!").put("data", list);
    }

    /**
     * 添加轮播图
     * @param appCarouselEntity
     * @return
     */
    @PostMapping("/addCarouse")
    public R addCarouse(@RequestBody AppCarouselEntity appCarouselEntity){
        boolean flag = appCarouseService.save(appCarouselEntity);
        if(flag){
            return R.ok("轮播图添加成功!");
        }
        return R.ok("轮播图添加失败!");
    }

    /**
     * 删除轮播图(逻辑删除)
     * @param appCarouselEntity
     * @return
     */
    @PostMapping("/removeCarouse")
    public R removeCarouse(@RequestBody AppCarouselEntity appCarouselEntity){
        boolean flag = appCarouseService.save(appCarouselEntity);
        if(flag){
            return R.ok("轮播图添加成功!");
        }
        return R.ok("轮播图添加失败!");
    }

    /**
     * 编辑轮播图
     * @param appCarouselEntity
     * @return
     */
    @PostMapping("/editCarouse")
    public R editCarouse(@RequestBody AppCarouselEntity appCarouselEntity){
        boolean flag = appCarouseService.updateById(appCarouselEntity);
        if(flag){
            return R.ok("轮播图编辑成功!");
        }
        return R.ok("轮播图编辑失败!");
    }

}
